package src;
import src.S1.demo1;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Main {
    public static JFrame init() {
        JFrame f = new JFrame("软件体系结构实验二-202031061530-林翰平");
        f.setVisible(true);
        f.setBounds(350, 100, 1280, 820);

        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        return f;
    }

    public void panelLoad(JFrame f) {

        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();

        jp1.setBackground(Color.GRAY);
        jp2.setBackground(Color.lightGray);
        jp1.setSize(1280, 120);
        jp2.setSize(1280, 700);
        jp1.setLayout(null);
        JButton btn1 = new JButton("主程序-子程序软件体系结构");
        JButton btn2 = new JButton("面向对象软件体系结构");
        JButton btn3 = new JButton("事件系统软件体系结构");
        JButton btn4 = new JButton("管道-过滤软件体系结构");
        JButton btnSummit = new JButton("开始转化");
        btn1.setBounds(60, 30, 200, 50);
        btn2.setBounds(280, 30, 200, 50);
        btn3.setBounds(500, 30, 200, 50);
        btn4.setBounds(720, 30, 200, 50);
        btnSummit.setBounds(1020, 30, 150, 50);

        jp1.add(btn1);
        jp1.add(btn2);
        jp1.add(btn3);
        jp1.add(btn4);
        jp1.add(btnSummit);

        f.add(jp1);
        f.add(jp2);

        //按钮点击事件
        btn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                drawP1();
                new MyDialog1();
            }
        });
        btn2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                drawP2();
                new MyDialog2();
            }
        });
        btn3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                drawP3();
                new MyDialog3();
            }
        });
        btn4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                drawP4();
                new MyDialog4();
            }
        });
        btnSummit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("转化成功!");
                demo1 demo = new demo1();
                demo.handle();
                new MyDialog5();
            }
        });

    }

    //设置弹窗
    class MyDialog1 extends JDialog {
        public MyDialog1() {
            this.setVisible(true);
            this.setBounds(100, 100, 500, 500);
            Container container = this.getContentPane();
            JTextArea jTextArea = new JTextArea("  \"package src.S1;\\n\" +\n" +
                    "                    \"\\n\" +\n" +
                    "                    \"import java.io.*;\\n\" +\n" +
                    "                    \"import java.util.*;\\n\" +\n" +
                    "                    \"\\n\" +\n" +
                    "                    \"public class demo1 {\\n\" +\n" +
                    "                    \"    private ArrayList<String> kwicList = new ArrayList<String>();\\n\" +\n" +
                    "                    \"    private ArrayList<String> lineTxt = new ArrayList<String>();\\n\" +\n" +
                    "                    \"    private BufferedReader inputFile;\\n\" +\n" +
                    "                    \"    private BufferedWriter outputFile;\\n\" +\n" +
                    "                    \"\\n\" +\n" +
                    "                    \"    public static void main(String[] args) {\\n\" +\n" +
                    "                    \"        demo1 kwic = new demo1();\\n\" +\n" +
                    "                    \"        kwic.input(\\\"E:\\\\\\\\input.txt\\\");\\n\" +\n" +
                    "                    \"        kwic.shift();\\n\" +\n" +
                    "                    \"        kwic.alphabetizer();\\n\" +\n" +
                    "                    \"        kwic.output(\\\"E:\\\\\\\\output.txt\\\");\\n\" +\n" +
                    "                    \"    }\\n\" +\n" +
                    "                    \"\\n\" +\n" +
                    "                    \"\\n\" +\n" +
                    "                    \"    public void handle(){\\n\" +\n" +
                    "                    \"        input(\\\"E:\\\\\\\\input.txt\\\");\\n\" +\n" +
                    "                    \"        shift();\\n\" +\n" +
                    "                    \"        alphabetizer();\\n\" +\n" +
                    "                    \"        output(\\\"E:\\\\\\\\output.txt\\\");\\n\" +\n" +
                    "                    \"    }\\n\" +\n" +
                    "                    \"    public void input(String fileName) {\\n\" +\n" +
                    "                    \"        try {\\n\" +\n" +
                    "                    \"            inputFile = new BufferedReader(new FileReader(fileName));\\n\" +\n" +
                    "                    \"        } catch (Exception e) {\\n\" +\n" +
                    "                    \"            e.printStackTrace();\\n\" +\n" +
                    "                    \"        }\\n\" +\n" +
                    "                    \"        String line;\\n\" +\n" +
                    "                    \"        try {\\n\" +\n" +
                    "                    \"            while ((line = inputFile.readLine()) != null) {\\n\" +\n" +
                    "                    \"                lineTxt.add(line);\\n\" +\n" +
                    "                    \"            }\\n\" +\n" +
                    "                    \"        } catch (IOException e) {\\n\" +\n" +
                    "                    \"            e.printStackTrace();\\n\" +\n" +
                    "                    \"        }\\n\" +\n" +
                    "                    \"    }\\n\" +\n" +
                    "                    \"\\n\" +\n" +
                    "                    \"    public void output(String filename){\\n\" +\n" +
                    "                    \"        Iterator<String> it = kwicList.iterator();\\n\" +\n" +
                    "                    \"        try {\\n\" +\n" +
                    "                    \"            outputFile = new BufferedWriter(new FileWriter(filename));\\n\" +\n" +
                    "                    \"            while (it.hasNext()) {\\n\" +\n" +
                    "                    \"                outputFile.write(it.next()+\\\"\\\\n\\\");\\n\" +\n" +
                    "                    \"            }\\n\" +\n" +
                    "                    \"        }catch (IOException e){\\n\" +\n" +
                    "                    \"            e.printStackTrace();\\n\" +\n" +
                    "                    \"        }finally {\\n\" +\n" +
                    "                    \"            try {\\n\" +\n" +
                    "                    \"                if (outputFile!=null) {\\n\" +\n" +
                    "                    \"                    outputFile.close();\\n\" +\n" +
                    "                    \"                }\\n\" +\n" +
                    "                    \"            } catch (IOException e) {\\n\" +\n" +
                    "                    \"                e.printStackTrace();\\n\" +\n" +
                    "                    \"            }\\n\" +\n" +
                    "                    \"        }\\n\" +\n" +
                    "                    \"    }\\n\" +\n" +
                    "                    \"\\n\" +\n" +
                    "                    \"    public void shift() {\\n\" +\n" +
                    "                    \"        //获取每个单词，存入tokens\\n\" +\n" +
                    "                    \"        Iterator<String> it = lineTxt.iterator();\\n\" +\n" +
                    "                    \"        while (it.hasNext()) {\\n\" +\n" +
                    "                    \"            StringTokenizer token = new StringTokenizer(it.next());\\n\" +\n" +
                    "                    \"            ArrayList<String> tokens = new ArrayList<String>();\\n\" +\n" +
                    "                    \"            int i = 0;\\n\" +\n" +
                    "                    \"            //循环添加单词\\n\" +\n" +
                    "                    \"            int count = token.countTokens();\\n\" +\n" +
                    "                    \"            while (i < count) {\\n\" +\n" +
                    "                    \"                tokens.add(token.nextToken());\\n\" +\n" +
                    "                    \"                i++;\\n\" +\n" +
                    "                    \"            }\\n\" +\n" +
                    "                    \"\\n\" +\n" +
                    "                    \"            //display(tokens);\\n\" +\n" +
                    "                    \"            //切割各个单词，不断改变起始值和利用loop实现位移。\\n\" +\n" +
                    "                    \"            for (i = 0; i < count; i++) {\\n\" +\n" +
                    "                    \"                StringBuffer lineBuffer = new StringBuffer();\\n\" +\n" +
                    "                    \"                int index = i;\\n\" +\n" +
                    "                    \"                for (int f = 0; f < count; f++) {\\n\" +\n" +
                    "                    \"                    //从头继续位移\\n\" +\n" +
                    "                    \"                    if (index >= count)\\n\" +\n" +
                    "                    \"                        index = 0;\\n\" +\n" +
                    "                    \"                    //存入StringBuffer\\n\" +\n" +
                    "                    \"                    lineBuffer.append(tokens.get(index));\\n\" +\n" +
                    "                    \"                    lineBuffer.append(\\\" \\\");\\n\" +\n" +
                    "                    \"                    index++;\\n\" +\n" +
                    "                    \"                }\\n\" +\n" +
                    "                    \"                String tmp = lineBuffer.toString();\\n\" +\n" +
                    "                    \"                kwicList.add(tmp);\\n\" +\n" +
                    "                    \"            }\\n\" +\n" +
                    "                    \"        }\\n\" +\n" +
                    "                    \"\\n\" +\n" +
                    "                    \"    }\\n\" +\n" +
                    "                    \"\\n\" +\n" +
                    "                    \"\\n\" +\n" +
                    "                    \"    public void alphabetizer() {\\n\" +\n" +
                    "                    \"        Collections.sort(this.kwicList, new AlphabetizerComparator());\\n\" +\n" +
                    "                    \"    }\\n\" +\n" +
                    "                    \"\\n\" +\n" +
                    "                    \"    private class AlphabetizerComparator implements Comparator<String> {\\n\" +\n" +
                    "                    \"        @Override\\n\" +\n" +
                    "                    \"        public int compare(String o1, String o2) {\\n\" +\n" +
                    "                    \"            if (o1 == null && o2 == null) {\\n\" +\n" +
                    "                    \"                throw new NullPointerException();\\n\" +\n" +
                    "                    \"            }\\n\" +\n" +
                    "                    \"            int compareValue = 0;\\n\" +\n" +
                    "                    \"            char o1c = o1.toLowerCase().charAt(0); //忽略大小写\\n\" +\n" +
                    "                    \"            char o2c = o2.toLowerCase().charAt(0); //忽略大小写\\n\" +\n" +
                    "                    \"            compareValue = o1c - o2c;\\n\" +\n" +
                    "                    \"            return compareValue;\\n\" +\n" +
                    "                    \"\\n\" +\n" +
                    "                    \"        }\\n\" +\n" +
                    "                    \"\\n\" +\n" +
                    "                    \"    }\\n\" +\n" +
                    "                    \"}\\n\";");
            container.add(jTextArea);
        }
    }
    class MyDialog2 extends JDialog {
        public MyDialog2() {
            this.setVisible(true);
            this.setBounds(100, 100, 500, 500);
            Container container = this.getContentPane();
            JTextArea jTextArea = new JTextArea("package src.S2;\n" +
                    "\n" +
                    "public class Main {\n" +
                    "    public static void main(String[] args) {\n" +
                    "        Input input = new Input();\n" +
                    "        input.input(\"E:\\\\input.txt\");\n" +
                    "        Shift shift = new Shift(input.getLineTxt());\n" +
                    "        shift.shift();\n" +
                    "        Alphabetizer alphabetizer = new Alphabetizer(shift.getKwicList());\n" +
                    "        alphabetizer.sort();\n" +
                    "        Output output = new Output(alphabetizer.getKwicList());\n" +
                    "        output.output(\"E:\\\\output2.txt\");\n" +
                    "\n" +
                    "    }\n" +
                    "}\n");
            container.add(jTextArea);
        }
    }
    class MyDialog3 extends JDialog {
        public MyDialog3() {
            this.setVisible(true);
            this.setBounds(100, 100, 500, 500);
            Container container = this.getContentPane();
            JTextArea jTextArea = new JTextArea("package src.S3;\n" +
                    "\n" +
                    "public class Main {\n" +
                    "    public static void main(String[] args) {\n" +
                    "        //创建主题\n" +
                    "        KWICSubject kwicSubject = new KWICSubject();\n" +
                    "        //创建观察者\n" +
                    "        Input input = new Input(\"E:\\\\input.txt\");\n" +
                    "        Shift shift = new Shift(input.getLineTxt());\n" +
                    "        Alphabetizer alphabetizer = new Alphabetizer(shift.getKwicList());\n" +
                    "        Output output = new Output(alphabetizer.getKwicList(), \"E:\\\\output3.txt\");\n" +
                    "\n" +
                    "        // 将观察者加入主题\n" +
                    "        kwicSubject.addObserver(input);\n" +
                    "        kwicSubject.addObserver(shift);\n" +
                    "        kwicSubject.addObserver(alphabetizer);\n" +
                    "        kwicSubject.addObserver(output);\n" +
                    "        // 逐步调用各个观察者\n" +
                    "        kwicSubject.startKWIC();\n" +
                    "    }\n" +
                    "}\n");
            container.add(jTextArea);
        }
    }
    class MyDialog4 extends JDialog {
        public MyDialog4() {
            this.setVisible(true);
            this.setBounds(100, 100, 500, 500);
            Container container = this.getContentPane();
            JTextArea jTextArea = new JTextArea("package src.S4;\n" +
                    "\n" +
                    "import java.io.File;\n" +
                    "import java.io.IOException;\n" +
                    "\n" +
                    "public class Main {\n" +
                    "    public static void main(String[] args) throws IOException {\n" +
                    "        File inFile = new File(\"E:\\\\input.txt\");\n" +
                    "        File outFile = new File(\"E:\\\\output4.txt\");\n" +
                    "        Pipe pipe1 = new Pipe();\n" +
                    "        Pipe pipe2 = new Pipe();\n" +
                    "        Pipe pipe3 = new Pipe();\n" +
                    "        Input input = new Input(inFile, pipe1);\n" +
                    "        Shift shift = new Shift(pipe1, pipe2);\n" +
                    "        Alphabetizer alphabetizer  = new Alphabetizer(pipe2, pipe3);\n" +
                    "        Output output = new Output(outFile,pipe3);\n" +
                    "        input.transform();\n" +
                    "        shift.transform();\n" +
                    "        alphabetizer.transform();\n" +
                    "        output.transform();\n" +
                    "\n" +
                    "    }\n" +
                    "}");

            container.add(jTextArea);
        }
    }
    class MyDialog5 extends JDialog {
        public MyDialog5() {
            this.setVisible(true);
            this.setBounds(800,400,200,150);
            Container container = this.getContentPane();
            JLabel jLabel = new JLabel("转化完毕!请查看文件(E)");
            container.add(jLabel);
        }
    }


        public void drawP1() {
            BufferedImage bufferedImage = null;
            try {
                bufferedImage = ImageIO.read(new File("C:\\Users\\jilin\\Pictures\\work2/p1.png"));
                ImageIcon icon = new ImageIcon(bufferedImage);
                JLabel label = new JLabel(icon);

                JOptionPane.showMessageDialog(null, label);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void drawP2() {
            BufferedImage bufferedImage = null;
            try {
                bufferedImage = ImageIO.read(new File("C:\\Users\\jilin\\Pictures\\work2/p2.png"));
                ImageIcon icon = new ImageIcon(bufferedImage);
                JLabel label = new JLabel(icon);

                JOptionPane.showMessageDialog(null, label);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void drawP3() {
            BufferedImage bufferedImage = null;
            try {
                bufferedImage = ImageIO.read(new File("C:\\Users\\jilin\\Pictures\\work2/p3.png"));
                ImageIcon icon = new ImageIcon(bufferedImage);
                JLabel label = new JLabel(icon);
                JOptionPane.showMessageDialog(null, label);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void drawP4() {
            BufferedImage bufferedImage = null;
            try {
                bufferedImage = ImageIO.read(new File("C:\\Users\\jilin\\Pictures\\work2/p4.png"));
                ImageIcon icon = new ImageIcon(bufferedImage);
                JLabel label = new JLabel(icon);
                JOptionPane.showMessageDialog(null, label);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public static void main(String[] args) throws IOException {
            Main i = new Main();
            i.panelLoad(i.init());


        }
}
